package mulMan.gui.controller;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;

import javax.swing.JFileChooser;

import mulMan.domain.AudioFile;
import mulMan.gui.view.AddAudioFileView;
import mulMan.gui.view.AudioFileView;
import mulMan.gui.view.ModifyAudioFileView;

public class AudioFileController extends Observable{
	
	private AudioFile audioFile;
	private AudioFileView view;
	
	
	public AudioFile getAudioFile() {
		return audioFile;
	}
	public void setAudioFile(AudioFile audioFile) {
		this.audioFile = audioFile;
	}

	public AudioFileView getView() {
		return view;
	}
	public void setView(AudioFileView view) {
		this.view = view;
	}
	
	
	public AudioFileController(AudioFile audioFile, AudioFileView view) {
		this.audioFile = audioFile;
		this.view = view;
		
		view.addFindAudioFileListener(new FindAudioFileListener());
		
		if (view instanceof ModifyAudioFileView) {
			((ModifyAudioFileView)view).addModifyAudioFileListener(new ModifyAudioFileListener());
			((ModifyAudioFileView)view).addPlayAudioFileListener(new PlayAudioFileListener());
		}
		
		if (view instanceof AddAudioFileView) {
			((AddAudioFileView)view).addAddAudioFileListener(new AddAudioFileListener());
		}
	}
	
	
	//ACTION LISTENERS
	
	class ModifyAudioFileListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	if (checkFields()) {
		    	try {
					audioFile.updateAudioFile();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
		    	((ModifyAudioFileView)view).dispose();
		    }
	    }
	}
	
	class PlayAudioFileListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	try {
				audioFile.play();
				view.getErrorLabel().setText("");
			} catch (Exception e1) {
				view.getErrorLabel().setText("The path or file format is invalid");
				e1.printStackTrace();
			}
	    }
	}
	
	class AddAudioFileListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	if (checkFields()) {
		    	try {
					audioFile.saveAudioFile();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
		    	((AddAudioFileView)view).getButtonAdd().setEnabled(false);
		    	((AddAudioFileView)view).dispose();
		    }
	    }
	}
	
	class FindAudioFileListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	int returnVal = view.getChooser().showOpenDialog(null);
		    if(returnVal == JFileChooser.APPROVE_OPTION) {
		       view.getPathTextField().setText(view.getChooser().getSelectedFile().getAbsolutePath());
		    }
	    }
	}
	

	public boolean checkFields() {
		view.resetErrors();
		boolean valid = true;
		
		if (view.getTitleTextField().getText().equals("")) {
			view.getTitleLabel().setForeground(Color.RED);
			valid = false;
		}
		if (view.getAlbumTextField().getText().equals("")) {
			view.getAlbumLabel().setForeground(Color.RED);
			valid = false;
		}
		if (view.getArtistTextField().getText().equals("")) {
			view.getArtistLabel().setForeground(Color.RED);
			valid = false;
		}
		if (!containsOnlyNumbers(view.getLengthTextField().getText())) {
			view.getLengthLabel().setForeground(Color.RED);
			valid = false;
		}
		if (view.getPathTextField().getText().equals("")) {
			view.getPathLabel().setForeground(Color.RED);
			valid = false;
		}
		
		
		if (valid) {
			audioFile.setTitle(view.getTitleTextField().getText());
			audioFile.setAlbum(view.getAlbumTextField().getText());
			audioFile.setArtist(view.getArtistTextField().getText());
			audioFile.setLength(Integer.valueOf(view.getLengthTextField().getText()));
			audioFile.setPath(view.getPathTextField().getText());
		} else {
			view.getErrorLabel().setText("The fields in red are invalid");
		}
		
		return valid;
	}
	
	public boolean containsOnlyNumbers(String str) {
        
        if (str == null || str.length() == 0) {
            return false;
        } else if (str.equals("0")) {
        	return false;
        }
        
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i)))
                return false;
        }
        
        return true;
    }
}
